package org.summer.gateway.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.summer.common.http.Result;
import org.summer.common.http.ResultCode;
import org.springframework.r2dbc.core.DatabaseClient;
import org.springframework.stereotype.Service;
import org.summer.gateway.dto.MysqlReplicaStatus;
import reactor.core.publisher.Mono;

import java.util.Map;
import java.util.Objects;

/**
 * 系统主要功能服务接口
 * @author wjj9132
 * @date 2024/9/27 14:51
 * @since 1.0
 */
@Log4j2
@Service
@RequiredArgsConstructor
public class MainService {
    private static final String SQL = "SHOW REPLICA STATUS";
    private final DatabaseClient databaseClient;

    /**
     * 获取mysql主从复制状态
     */
    public Mono<Result<MysqlReplicaStatus>> findMysqlReplicaStatus() {
        log.debug("获取mysql主从复制状态信息");
        return this.databaseClient.sql(SQL)
                .fetch()
                .one()
                .map(this::mapToMysqlReplicaStatus)
                .map(e -> Result.ok(ResultCode.SUCCESS, e));
    }

    private MysqlReplicaStatus mapToMysqlReplicaStatus(Map<String, Object> map) {
        MysqlReplicaStatus res = new MysqlReplicaStatus();
        res.setReplicaIOState(Objects.toString(map.get("Replica_IO_State")));
        res.setSourceHost(Objects.toString(map.get("Source_Host")));
        res.setSourceUser(Objects.toString(map.get("Source_User")));
        res.setSourcePort(Integer.parseInt(Objects.toString(map.get("Source_Port"))));
        res.setConnectRetry(Integer.parseInt(Objects.toString(map.get("Connect_Retry"))));
        res.setSourceLogFile(Objects.toString(map.get("Source_Log_File")));
        res.setReadSourceLogPos(Integer.parseInt(Objects.toString(map.get("Read_Source_Log_Pos"))));
        res.setRelayLogFile(Objects.toString(map.get("Relay_Log_File")));
        res.setRelayLogPos(Integer.parseInt(Objects.toString(map.get("Relay_Log_Pos"))));
        res.setRelaySourceLogFile(Objects.toString(map.get("Relay_Source_Log_File")));
        res.setReplicaIORunning(Objects.toString(map.get("Replica_IO_Running")));
        res.setReplicaSQLRunning(Objects.toString(map.get("Replica_SQL_Running")));
        res.setLastErrno(Integer.parseInt(Objects.toString(map.get("Last_Errno"))));
        res.setLastIOErrno(Integer.parseInt(Objects.toString(map.get("Last_IO_Errno"))));
        res.setLastSQLErrno(Integer.parseInt(Objects.toString(map.get("Last_SQL_Errno"))));
        res.setLastError(Objects.toString(map.get("Last_Error")));
        res.setLastIOError(Objects.toString(map.get("Last_IO_Error")));
        res.setLastSQLError(Objects.toString(map.get("Last_SQL_Error")));
        res.setExecSourceLogPos(Integer.parseInt(Objects.toString(map.get("Exec_Source_Log_Pos"))));
        res.setRelayLogSpace(Integer.parseInt(Objects.toString(map.get("Relay_Log_Space"))));
        res.setSourceServerId(Integer.parseInt(Objects.toString(map.get("Source_Server_ID"))));
        res.setSourceUUID(Objects.toString(map.get("Source_UUID")));
        res.setSourceInfoFile(Objects.toString(map.get("Source_Info_File")));
        res.setSqlLDelay(Integer.parseInt(Objects.toString(map.get("SQL_Delay"))));
        res.setReplicaSQLRunningState(Objects.toString(map.get("Replica_SQL_Running_State")));
        res.setSourceRetryCount(Integer.parseInt(Objects.toString(map.get("Source_Retry_Count"))));
        res.setAutoPosition(Integer.parseInt(Objects.toString(map.get("Auto_Position"))));
        return res;
    }


}
